/*
 * Copyright (C) 2004, Velocityme Partnership
 * AdvancedSearchActionForm.java
 *
 * Created on 23 December 2004, 03:07
 */

package com.velocityme.www.actionforms;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class AdvancedSearchActionForm extends ActionForm {
    
    private Long m_nodeId;
    private String m_ids;
    private String m_keywords;
    private Set<Long> m_statusIds;
    private Set<Long> m_contactableIds;
    private boolean m_includeStart;
    private boolean m_includeStop;
    private int m_startYear;
    private int m_startMonth;
    private int m_startDay;
    private int m_stopYear;
    private int m_stopMonth;
    private int m_stopDay;
    
    /** Creates a new instance of AdvancedSearchActionForm */
    public AdvancedSearchActionForm() {
    }
    
    @Override
    public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
        m_includeStart = new Boolean(false);
        m_includeStop = new Boolean(false);
    }
    
    public Long getNodeId() { return m_nodeId; }
    public void setNodeId(Long nodeId) { m_nodeId = nodeId; }
    
    public String getIds() { return m_ids; }
    public void setIds(String ids) { m_ids = ids; }
    
    public String getKeywords() { return m_keywords; }
    public void setKeywords(String keywords) { m_keywords = keywords; }
    
    public Set<Long> getStatusIds() { return m_statusIds; }
    public void setStatusIds(Set<Long> statusIds) { m_statusIds = statusIds; }
    
    public Set<Long> getContactableIds() { return m_contactableIds; }
    public void setContactableIds(Set<Long> contactableIds) { m_contactableIds = contactableIds; }
    
    public Boolean getIncludeStart() { return m_includeStart; }
    public void setIncludeStart(Boolean includeStart) { m_includeStart = includeStart; }
    
    public Boolean getIncludeStop() { return m_includeStop; }
    public void setIncludeStop(Boolean includeStop) { m_includeStop = includeStop; }
    
    public int getStartYear() { return m_startYear; }
    public void setStartYear(int year) { m_startYear = year; }
    public int getStartMonth() { return m_startMonth; }
    public void setStartMonth(int month) { m_startMonth = month; }
    public int getStartDay() { return m_startDay; }
    public void setStartDay(int day) { m_startDay = day; }
    public int getStopYear() { return m_stopYear; }
    public void setStopYear(int year) { m_stopYear = year; }
    public int getStopMonth() { return m_stopMonth; }
    public void setStopMonth(int month) { m_stopMonth = month; }
    public int getStopDay() { return m_stopDay; }
    public void setStopDay(int day) { m_stopDay = day; }    
    
    public Set<Long> getIdList() {
        Set<Long> result = new HashSet<Long>();
        if (m_ids.length() > 0) {
            String[] numbers = Pattern.compile("\\D+").split(m_ids);
            for (int i = 0; i < numbers.length; i++) {
                result.add(Long.decode(numbers[i]));
            }
        }
        return result;
    }
    
    public Set<String> getKeywordsArray() {
        Set<String> result = new HashSet<String>();
        if (m_keywords.length() > 0) {
            String[] strings =  Pattern.compile("\\s+").split(m_keywords);
            result.addAll(Arrays.asList(strings));
        }
        return result;
    }
    
    public Date getStartDate() {
        if (m_includeStart == true) {
            Calendar startDate = Calendar.getInstance(); startDate.clear();
            startDate.set(m_startYear, m_startMonth, m_startDay);
            return startDate.getTime();
        }
        else
            return null;
    }
    
    public Date getStopDate() {
        if (m_includeStop == true) {
            Calendar stopDate = Calendar.getInstance(); stopDate.clear();
            stopDate.set(m_stopYear, m_stopMonth, m_stopDay, 23, 59, 59);
            return stopDate.getTime();
        }
        else
            return null;
    }
    
}
